package com.softa.service.system.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageInfo;
import com.softa.annotation.ServiceLog;
import com.softa.bean.DataGridModel;
import com.softa.dao.dto.SysUserDTO;
import com.softa.dao.mapper.base.SysServerMapper;
import com.softa.dao.mapper.base.SysUserMapper;
import com.softa.dao.mapper.relation.system.SysServerRelationMapper;
import com.softa.dao.mapper.relation.system.SysUserRelationMapper;
import com.softa.dao.model.SysResource;
import com.softa.dao.model.SysRole;
import com.softa.dao.model.SysServer;
import com.softa.dao.model.SysUser;
import com.softa.service.encrypt.PasswordHelper;
import com.softa.service.system.RoleService;
import com.softa.service.system.SysServerService;
import com.softa.service.system.UserService;
import com.softa.vo.DataGridRequest;
import com.softa.vo.QueryRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class SysServerServiceImpl implements SysServerService {

    @Resource
    private SysServerMapper sysServerMapper;
    @Resource
    private SysServerRelationMapper sysServerRelationMapper;

    @ServiceLog(value="创建服务器")
    public String createSysServer(SysServer sysServer) {
        DataGridModel dataGrid = new DataGridModel();
        dataGrid.getParams().put("name", sysServer.getName());
        boolean isName=this.isExists(dataGrid);
        String str="录入成功";
        if(isName){
            str="1";//存在名称
        }else{
            sysServerMapper.insert(sysServer);
        }
        return str;
    }

    @ServiceLog(value="更新服务器")
    public String updateSysServer(SysServer sysServer) {
        DataGridModel dataGrid = new DataGridModel();
        dataGrid.getParams().put("id", sysServer.getId());
        dataGrid.getParams().put("name", sysServer.getName());
        boolean isName = this.isExists(dataGrid);
        String str = "修改成功";
        if (isName) {
            str = "1";//存在名称
        } else {
            sysServerMapper.updateByPrimaryKey(sysServer);
        }
        return str;
    }

    @ServiceLog(value="删除用户")
    public void deleteSysServer(Long serverId) {
        sysServerMapper.deleteByPrimaryKey(serverId);
    }

    @Override
    public PageInfo<SysServer> queryUserByParam(DataGridRequest request, final QueryRequest queryRequest) {
	  PageInfo<SysServer> page = request.startPagePlugin().doSelectPageInfo(new ISelect() {
		@Override
		public void doSelect() {
		    sysServerRelationMapper.selectByCondition(queryRequest);
		}
	  });
	  return  page;
    }

    public boolean isExists(DataGridModel dataGrid){
        List<SysServer> list=sysServerRelationMapper.selectByParam(dataGrid);
        boolean flag=false;
        if(list.size() > 0){
            flag=true;
        }
        return flag;
    }
}
